// pages/titleCategory/titleCategory.js

import {ajax} from "../../utils/httpRequest.js";
import {URI_TITLEDETAIL} from "../../api.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    getUrlParams: {},
    icon: '',
    sort: 'zuixin',//默认
    sort_type: -1,
    reqPage: 1,
    limit: 20,
    sw: true,
    show_loading:false,
    text:'正在加载····',
    showGoTop:false,
    goods_list: [],
    show_noData:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      getUrlParams: options
    });
    wx.setNavigationBarTitle({
      title: options.title
    })
    this.getGoodsList()
  },

  getGoodsList: function () {
    var that = this;
    ajax.get(URI_TITLEDETAIL, {
      key: that.data.getUrlParams.key,
      orderByTile:that.data.sort == 'zuixin'? '':that.data.sort,
      orderBy:that.data.sort_type==-1?'':that.data.sort_type,
      page:that.data.reqPage,
      accessToken: wx.getStorageSync('accessToken')
    }, {}, (res) => {
      if (!that.data.icon) {
        that.setData({
          icon: res.data.data.ico
        })
      }
      that.setData({
        goods_list: res.data.data.commodityList,
        show_loading:false,
        text:'正在加载····',
      });

      that.watchPageData();

    })
  },

  getMoreGoodsList() {
    var that = this;
    ajax.get(URI_TITLEDETAIL, {
      key: that.data.getUrlParams.key,
      orderByTile:that.data.sort == 'zuixin'? '':that.data.sort,
      orderBy:that.data.sort_type==-1?'':that.data.sort_type,
      page: that.data.reqPage,
      accessToken: wx.getStorageSync('accessToken')
    }, {}, (res) => {
      if(res.data.data.commodityList.length){
        that.setData({
          goods_list: that.data.goods_list.concat(res.data.data.commodityList),
          sw: true,
          show_loading:false
        });
      }
      if (res.data.data.commodityList.length < that.data.limit) {
        that.setData({
          sw: false,
          text:'---暂无数据---'
        })
        setTimeout(()=>{
          that.setData({
            show_loading:false
          });
        },1000)
      }

      // console.log(that.data.goods_list);
    },(err)=>{
      console.log(err);
    })
  },


  sortClick: function (e) {
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。',
        confirmColor: '#ff474f'
      })
    }
    var page = this;
    var sort = e.currentTarget.dataset.sort;
    var default_sort_type = e.currentTarget.dataset.default_sort_type == undefined ? -1 : e.currentTarget.dataset.default_sort_type;
    var sort_type = page.data.sort_type;
    if (page.data.sort == sort) {
      if (default_sort_type == -1) {
        return;
      }
      if (page.data.sort_type == -1) {
        sort_type = default_sort_type;
      } else {
        sort_type = (sort_type == 'desc' ? 'asc' : 'desc');
      }
    } else {
      sort_type = default_sort_type;
    }

    page.setData({
      sort: sort,
      sort_type: sort_type,
      reqPage:1
    });
    page.getGoodsList();
  }

  ,
  toDetails:function (e) {
    var item = e.currentTarget.dataset.item;
    //console.log(e.currentTarget.dataset.item);
    var data= JSON.stringify(e.currentTarget.dataset.item);
    wx.navigateTo({
      url: `/pages/details/details?goodId=${item.goodsId}&title=${item.title}&md5=${item.md5}&platform=${item.platform}&item=${data}`
    })

  },

  //判断有没数据；
  watchPageData(){
    if(this.data.goods_list && this.data.goods_list.length>0){
      this.setData({
        show_noData:false
      })
    } else {
      this.setData({
        show_noData:true
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  }
  ,

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  }
  ,

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  }
  ,

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  }
  ,

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  }
  ,

  goTop: function (e) {
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。',
        confirmColor: '#ff474f'
      })
    }
  },

  onPageScroll(e){
    var scroll = e.scrollTop;
    if(scroll>500){
      this.setData({showGoTop:true})
    }else{
      this.setData({showGoTop:false})
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      show_loading:true
    })
    if (this.data.sw) {
      this.setData({
        reqPage: this.data.reqPage + 1
      })
      this.getMoreGoodsList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})